package com.hbzhit.common.utils;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hbzhit.common.entity.PageProcessQueryEntity;
import com.hbzhit.common.entity.ProcessEntity;
import com.hbzhit.modules.act.factory.WorkFlowFactory;
import com.hbzhit.modules.sys.entity.SysUserEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * 流程查询构造器封装
 *
 * @author Mark sunlightcs@gmail.com
 */
public class QueryProcess<T extends ProcessEntity> extends Query<T> {

    /**
     * 流程查询构造器封装
     * @param wrapper 条件构造器
     * @param processKey 流程key
     * @param queryForm 查询表单对象
     * @return 查询条件构造器
     */
    public LambdaQueryWrapper<T> getQuery(LambdaQueryWrapper<T> wrapper, String processKey, PageProcessQueryEntity queryForm) {

        SysUserEntity user = ShiroUtils.getUser();

        // 通用查询条件
        wrapper.orderByDesc(T::getProcessNo);
        wrapper.like(StrUtil.isNotBlank(queryForm.getStatusName()), T::getStatusName, queryForm.getStatusName())
                .inSql(StrUtil.isNotBlank(queryForm.getApplicantName()), T::getApplicant,
                        StrUtil.format("select id from sys_user where is_del=0 and real_name like '%{}%'", queryForm.getApplicantName()));

        // 查询待办已办
        // 管理员 最大权限
        if (user.getId() == Constant.SUPER_ADMIN) {
            List<String> allProcessIds = WorkFlowFactory.allProcessIds(processKey);
            if (allProcessIds == null || allProcessIds.size() == 0) {
                allProcessIds = new ArrayList<>();
                allProcessIds.add("0");
            }
            if (queryForm.getIsHistory() == Constant.INT_N) {
                wrapper.in(T::getProcessId, allProcessIds);
            } else if (queryForm.getIsHistory() == Constant.INT_Y) {
                wrapper.notIn(T::getProcessId, allProcessIds);
            }
            return wrapper;
        }
        // 普通用户
        if (queryForm.getIsHistory() == Constant.INT_N) {
            // 所有待办
            List<String> processIds = new ArrayList<>();
            processIds.add("0");

            // 查询角色待办
            List<String> groupProcessIds = WorkFlowFactory.groupProcessIds(processKey);
            if (groupProcessIds != null && groupProcessIds.size() > 0) {
                processIds.addAll(groupProcessIds);
            }

            // 查询用户待办
            List<String> userProcessIds = WorkFlowFactory.userProcessIds(processKey);
            if (userProcessIds != null && userProcessIds.size() > 0) {
                processIds.addAll(userProcessIds);
            }
            wrapper.in(T::getProcessId, processIds);

        } else if (queryForm.getIsHistory() == Constant.INT_Y) {
            // 查询已办
            List<String> myHistoricProcessIds = WorkFlowFactory.myHistoricProcessIds(processKey);
            if (myHistoricProcessIds == null || myHistoricProcessIds.size() == 0) {
                myHistoricProcessIds = new ArrayList<>();
                myHistoricProcessIds.add("0");
            }

            wrapper.in(T::getProcessId, myHistoricProcessIds);

            return wrapper;
        }

        return new QueryBase<T>().getQuery(wrapper, queryForm);
    }
}
